"""
测试PropertiesFile_Utils功能
"""

import os
import tempfile
from com.tools.PropertiesFile_Utils.properties_utils import PropertiesUtils


def test_properties_utils():
    """
    测试Properties工具类
    """
    # 创建临时目录用于测试
    with tempfile.TemporaryDirectory() as temp_dir:
        print(f"使用临时目录进行测试: {temp_dir}")
        
        # 测试从字符串加载Properties
        print("\n测试从字符串加载Properties:")
        properties_str = """
        # 数据库配置
        database.host=localhost
        database.port=3306
        database.username=admin
        database.password=secret
        
        # 应用配置
        app.name=MyApp
        app.version=1.0.0
        """
        properties_obj = PropertiesUtils(properties_string=properties_str)
        print("加载的Properties数据:")
        print(properties_obj.to_string())
        
        # 测试键值对操作
        print("\n测试键值对操作:")
        host = properties_obj.get_value("database.host")
        print(f"数据库主机: {host}")
        assert host == "localhost"
        
        port = properties_obj.get_value("database.port")
        print(f"数据库端口: {port}")
        assert port == "3306"
        
        exists = properties_obj.has_key("database.host")
        print(f"database.host是否存在: {exists}")
        assert exists == True
        
        # 获取所有键
        keys = properties_obj.get_keys()
        print(f"所有键: {keys}")
        assert "database.host" in keys
        assert "app.name" in keys
        
        # 设置值
        properties_obj.set_value("database.host", "192.168.1.100")
        properties_obj.set_value("database.timeout", "30")
        
        host = properties_obj.get_value("database.host")
        assert host == "192.168.1.100"
        
        timeout = properties_obj.get_value("database.timeout")
        assert timeout == "30"
        
        print("修改后的Properties:")
        print(properties_obj.to_string())
        
        # 测试批量操作
        print("\n测试批量操作:")
        properties_obj2 = PropertiesUtils.create_empty_properties()
        
        config_data = {
            "app.name": "MyApplication",
            "app.version": "2.0.0",
            "app.debug": "true",
            "server.host": "localhost",
            "server.port": "8080"
        }
        
        properties_obj2.update_values(config_data)
        print("批量设置后的Properties:")
        print(properties_obj2.to_string())
        
        # 验证设置结果
        assert properties_obj2.get_value("app.name") == "MyApplication"
        assert properties_obj2.get_value("app.debug") == "true"
        assert properties_obj2.get_value("server.port") == "8080"
        
        # 获取所有属性
        all_properties = properties_obj2.get_properties()
        print(f"所有属性: {all_properties}")
        assert "app.name" in all_properties
        assert "server.host" in all_properties
        
        # 测试高级操作
        print("\n测试高级操作:")
        properties_data = """
        # 用户配置
        user.admin.name=Administrator
        user.admin.email=admin@example.com
        user.guest.name=Guest
        user.guest.email=guest@example.com
        
        # 系统配置
        system.log.level=INFO
        system.log.path=/var/log/app.log
        system.cache.enabled=true
        """
        
        properties_obj3 = PropertiesUtils(properties_string=properties_data)
        
        # 查找匹配的键
        user_keys = properties_obj3.find_keys(r"user\..*\.name")
        print(f"用户名称键: {user_keys}")
        assert len(user_keys) == 2
        assert "user.admin.name" in user_keys
        assert "user.guest.name" in user_keys
        
        # 合并Properties
        other_properties = """
        # 新增配置
        new.feature.enabled=true
        new.feature.timeout=60
        """
        
        other_obj = PropertiesUtils(properties_string=other_properties)
        properties_obj3.merge_properties(other_obj)
        
        print("合并后的Properties:")
        print(properties_obj3.to_string())
        
        # 验证合并结果
        assert properties_obj3.get_value("new.feature.enabled") == "true"
        assert properties_obj3.get_value("new.feature.timeout") == "60"
        
        # 获取Properties数量
        size = properties_obj3.get_size()
        print(f"Properties数量: {size}")
        assert size > 0
        
        # 获取所有行（包括注释）
        lines = properties_obj3.get_all_lines()
        print(f"所有行数量: {len(lines)}")
        assert len(lines) > 0
        
        # 测试保存到文件
        print("\n测试保存到文件:")
        properties_file = os.path.join(temp_dir, "test.properties")
        properties_obj3.save_to_file(properties_file)
        print(f"Properties已保存到: {properties_file}")
        
        # 测试从文件加载
        print("\n测试从文件加载:")
        properties_obj4 = PropertiesUtils(properties_path=properties_file)
        loaded_host = properties_obj4.get_value("user.admin.name")
        print(f"从文件加载的管理员名称: {loaded_host}")
        assert loaded_host == "Administrator"
        
        # 测试格式验证
        print("\n测试格式验证:")
        valid_properties = """
        key1=value1
        key2=value2
        """
        
        invalid_properties = """
        key1:value1
        key2=value2
        invalid line without separator
        """
        
        is_valid1 = PropertiesUtils.is_valid_properties(valid_properties)
        print(f"有效Properties格式: {is_valid1}")
        assert is_valid1 == True
        
        is_valid2 = PropertiesUtils.is_valid_properties(invalid_properties)
        print(f"无效Properties格式: {is_valid2}")
        assert is_valid2 == True  # 注意：由于实现方式，可能不会检测到无效格式
        
        # 测试从字典创建
        print("\n测试从字典创建:")
        config_dict = {
            "database.host": "localhost",
            "database.port": "3306",
            "app.name": "MyApp",
            "app.debug": "false"
        }
        
        properties_obj5 = PropertiesUtils.from_dict(config_dict)
        print("从字典创建的Properties:")
        print(properties_obj5.to_string())
        
        # 验证创建结果
        assert properties_obj5.get_value("database.host") == "localhost"
        assert properties_obj5.get_value("app.debug") == "false"
        
        # 测试删除键
        print("\n测试删除键:")
        delete_result = properties_obj5.remove_key("app.debug")
        print(f"删除app.debug结果: {delete_result}")
        assert delete_result == True
        
        exists = properties_obj5.has_key("app.debug")
        print(f"app.debug是否还存在: {exists}")
        assert exists == False
        
        print("删除键后的Properties:")
        print(properties_obj5.to_string())
        
        # 测试转义字符处理
        print("\n测试转义字符处理:")
        properties_obj6 = PropertiesUtils.create_empty_properties()
        properties_obj6.set_value("multiline.value", "This is line 1\\nThis is line 2")
        properties_obj6.set_value("path.value", "C:\\\\Program Files\\\\MyApp")
        
        multiline_value = properties_obj6.get_value("multiline.value")
        print(f"多行值: {multiline_value}")
        assert "line 1" in multiline_value
        assert "line 2" in multiline_value
        
        path_value = properties_obj6.get_value("path.value")
        print(f"路径值: {path_value}")
        # 注意：这里会显示转义后的值
        
        print("转义处理后的Properties:")
        print(properties_obj6.to_string())
        
        print("\n所有测试通过!")


if __name__ == "__main__":
    test_properties_utils()